---@class GT.ui.control.skill_btn : GT.ui
---@field ["技能等级"] GT.ui.control.label 技能等级
---@field ["技能框"] GT.ui.control.image 技能框
---@field ["技能cd"] GT.ui.control.progress 技能cd
---@field ["技能图标"] GT.ui.control.image 技能图标
---@field ["升级按钮"] GT.ui.control.button 升级按钮
---@field ["技能层数"] GT.ui.control.label 技能层数
---@field ["技能消耗不足"] GT.ui.control.image 技能消耗不足
---@field ["技能禁用"] GT.ui.control.image 技能禁用
---@field ["冷却显示"] GT.ui.control.label 冷却显示
---@field slot_binding const.SlotType 槽位类型 槽位  ['PKG'] = 0,['BAR'] = 1,
---@field cast_key KeyboardKey 智能施法快捷键
---@field key KeyboardKey 快捷键
---@field skill GT.ability 技能
---@field sk_btn_leave UI_EVENT 离开事件
---@field sk_btn_enter UI_EVENT 进入事件
---@field u table<RoleID,GT.ui.control.skill_btn> 玩家独立数据驱动
GT.ui.control.skill_btn = class(GT.ui, false)


---@alias UI_EVENT_skbtn fun(data:any,player:GT.player,ui:GT.ui.control.skill_btn):void

---@class ui_data_skill_btn : ui_data
---@field slot_binding const.SlotType 槽位类型 槽位  ['PKG'] = 0,['BAR'] = 1,
---@field cast_key KeyboardKey  智能施法快捷键
---@field key KeyboardKey 快捷键
---@field skill GT.ability 技能
---@field sk_btn_leave UI_EVENT_skbtn 离开事件
---@field sk_btn_enter UI_EVENT_skbtn 进入事件


--- 创建技能ui
---@param data ui_data_skill_btn
---@return GT.ui.control.skill_btn
function GT.ui.control.skill_btn:ctor(data)
    data.class = GT.ui.control.skill_btn
    if data.uid ~= nil then
        super(self, data)
    else
        data.u_type = GT.ui.type.skill_btn
        self.slot_binding = data.slot_binding
        self.cast_key = data.cast_key
        self.skill = data.skill
        self.key = data.key
        self.sk_btn_leave = data.sk_btn_leave
        self.sk_btn_enter = data.sk_btn_enter
        --创建控件
        super(self, data)
        self.w = self:get_w() or 100
        self.h = self:get_h() or 100



        if self.slot_binding ~= nil then
            self:set_slot(self.slot_binding)
        end
        if self.cast_key ~= nil then
            self:set_skill_btn_smart_cast_key(self.cast_key)
        end
        if self.skill ~= nil then
            self:set_skill_on(self.skill)
        end
        if self.key ~= nil then
            self:set_key(self.key)
        end
        
    end

    self["技能等级"] = GT.ui.control.label.obj({
        player = self.player,
        uid = self:get_child_uid("skill_level_label"),
        is_data_driven = true
    })
    self["技能等级"].binding.show = false

    self["技能框"] = GT.ui.control.image.obj({
        player = self.player,
        uid = self:get_child_uid("skill_frame_img"),
        is_data_driven = true
    })


    self["技能cd"] = GT.ui.control.progress.obj({
        player = self.player,
        uid = self:get_child_uid("skill_cd_progress"),
        is_data_driven = true
    })

    self["技能图标"] = GT.ui.control.image.obj({
        player = self.player,
        uid = self:get_child_uid("skill_icon_img")
    })


    self["升级按钮"] = GT.ui.control.button.obj({
        player = self.player,
        uid = self:get_child_uid("skill_upgrade_btn"),
        is_data_driven = true
    })
    self["升级按钮"].binding.x = 50
    self["升级按钮"].binding.y = 50
    self["升级按钮"].binding.w = self.w
    self["升级按钮"].binding.h = self.w
    self["升级按钮"].binding.img = 100350

    self["技能层数"] = GT.ui.control.label.obj({
        player = self.player,
        uid = self:get_child_uid("skill_stack_label"),
        is_data_driven = true
    })

    self["技能消耗不足"] = GT.ui.control.image.obj({
        player = self.player,
        uid = self:get_child_uid("skill_consume_mask_img"),
        is_data_driven = true
    })
    self["技能消耗不足"].binding.img = 130011

    self["技能禁用"] = GT.ui.control.image.obj({
        player = self.player,
        uid = self:get_child_uid("skill_disable_img"),
        is_data_driven = true
    })
    self["技能禁用"].binding.z_order = 999

    self["冷却显示"] = GT.ui.control.label.obj({
        player = self.player,
        uid = self:get_child_uid("skill_cd_label"),
        is_data_driven = true
    })

    self["快捷键"] = GT.ui.control.label.obj({
        player = self.player,
        uid = self:get_child_uid("skill_shortcut_label"),
        is_data_driven = true
    })
    if data.uid~=nil then
        if self.sk_btn_enter ~= nil then
            self:skill_btn_enter(self.sk_btn_enter)
        end
        if self.sk_btn_leave ~= nil then
            self:skill_btn_leave(self.sk_btn_leave)
        end
    end

    self:skill_btn_watch()
    return self
end

---转对象
---@param data ui_data_skill_btn
---@return GT.ui.control.skill_btn
function GT.ui.control.skill_btn.obj(data)
    return new(GT.ui.control.skill_btn, data)
end

---进入事件
---@param fun UI_EVENT_skbtn 执行事件
function GT.ui.control.skill_btn:skill_btn_enter(fun)
    self["技能图标"]:enter(function(data, player)
        fun(data, player, self)
    end)
end

---离开事件
---@param fun UI_EVENT_skbtn 执行事件
function GT.ui.control.skill_btn:skill_btn_leave(fun)
    self["技能图标"]:leave(function(data, player)
        fun(data, player, self)
    end)
end

--监听绑定
---@return GT.ui.control.skill_btn
function GT.ui.control.skill_btn:skill_btn_watch()
    ---@type GT.ui.control.skill_btn
    self.binding = self.bind.bindable
    self.bind:add(
        {
            {
                key = "slot_binding",
                func = function(val, old)
                    self.slot_binding = val
                    self:set_slot(self.slot_binding)
                end
            },
            {
                key = "cast_key",
                func = function(val, old)
                    self.cast_key = val
                    self:set_skill_btn_smart_cast_key(self.cast_key)
                end
            },
            {
                key = "skill",
                func = function(val, old)
                    self.skill = val
                    if self.skill ~= nil then
                        self:set_skill_on(val)
                    else
                        if old ~= nil then
                            self:unbind_skill_on_ui_comp(old)
                        end
                    end
                end
            },
            {
                key = "key",
                func = function(val, old)
                    self.key = val
                    self:set_key(self.key)
                end
            },
        }
    )
    return self
end



---绑定技能对象到控件
---@param  skill GT.ability 技能对象
---@return GT.ui.control.skill_btn
function GT.ui.control.skill_btn:set_skill_on(skill)
    game_api.set_skill_on_ui_comp(self.player.player, skill.ability, self.uid)
end

--- 解绑技能实体到控件
---@param ability GT.ability 技能对象
function GT.ui.control.skill_btn:unbind_skill_on_ui_comp(ability)
    game_api.unbind_skill_on_ui_comp(self.player.player, ability.ability, self.uid)
end

---@param  key integer 快捷键
--设置按钮快捷键
function GT.ui.control.skill_btn:set_key(key)
    game_api.set_btn_short_cut(self.player.player, self.uid, key)
end

---设置智能施法快捷键
---@param  key number 快捷键
---@return GT.ui.control.skill_btn
function GT.ui.control.skill_btn:set_skill_btn_smart_cast_key(key)
    game_api.set_skill_btn_smart_cast_key(self.player.player, self.uid, key)
    return self
end

---设置智能施法组合快捷键
---@param  key number 辅助按键
---@return GT.ui.control.skill_btn
function GT.ui.control.skill_btn:set_skill_btn_func_meta_key(key)
    game_api.set_skill_btn_func_smart_cast_key(self.player.player, self.uid, key)
    return self
end

